package com.service;

import com.dao.CustomerDao;
import com.javabean.Customer;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import static com.mysql.cj.conf.PropertyKey.logger;
import static com.util.JDBCTools.getConnection;


/**
 * 这是一个具有管理功能的功能类. 内部数据不允许外部随意修改, 具有更好的封装性.
 */
public class CustomerService {
    private CustomerDao customerDao = new CustomerDao();

    /**
     * 用途：返回所有客户对象
     * 返回：集合
     */
    public List<Customer> getList() {
        try {
            return customerDao.listCustomer();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 用途：添加新客户
     * 参数：customer指定要添加的客户对象
     */
    public void addCustomer(Customer customer) {
        try {
            customerDao.insertCustomer(customer);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 修改指定id号的客户对象的信息
     *
     * @param customer 对象
     * @return 修改成功返回true, false表明指定id的客户未找到
     */
    public boolean updateCustomer(Customer customer) {
        int row = 0;
        try {
            row = customerDao.updateCustomer(customer);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return row > 0;
    }

    /**
     * 用途：删除指定id号的的客户对象记录
     * 参数： id 要删除的客户的id号
     * 返回：删除成功返回true；false表示没有找到
     */
    public boolean deleteCustomer(int id) {
        int row = 0;
        try {
            row = customerDao.deleteCustomer(id);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return row > 0;
    }

    public List<Customer> getCustomers(String user) {
        try {
//            System.out.println(getCustomers(user));
            return customerDao.getCustomer(user);
        } catch (Exception e) {
            throw new RuntimeException(e);
    }
    }
 /*   public List<Customer>isValidUser1 (String user,String MD5password) {
        try {
            if (customerDao.getCustomer(user)>=1){

            }

            return customerDao.getCustomer(user);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
*/
    public boolean isValidUser(String userAccount, String MD5password) {
        boolean isValid = false;
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        try {
            connection = getConnection(); // 假设这是获取数据库连接的方法，你需要根据实际情况实现
            String sql = "SELECT COUNT(*) FROM user WHERE user =? AND password =?";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, userAccount);
            preparedStatement.setString(2, MD5password);
            resultSet = preparedStatement.executeQuery();
            if (resultSet.next()) {
                int count = resultSet.getInt(1);
                isValid = count > 0;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 关闭资源，释放连接等，防止资源泄漏
            try {
                if (resultSet!= null) {
                    resultSet.close();
                }
                if (preparedStatement!= null) {
                    preparedStatement.close();
                }
                if (connection!= null) {
                    connection.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return isValid;
    }
}
